package com.example.javaairportflightanalysis.service.impl;

import com.example.javaairportflightanalysis.dao.AirLineMapper;
import com.example.javaairportflightanalysis.dao.BaseApueMapper;
import com.example.javaairportflightanalysis.dao.DfdlMapper;
import com.example.javaairportflightanalysis.domin.dto.BaseApueCnnm;
import com.example.javaairportflightanalysis.domin.dto.DfdlDto2;
import com.example.javaairportflightanalysis.domin.entity.AirLine;
import com.example.javaairportflightanalysis.domin.merge.DfdlDto2Merge;
import com.example.javaairportflightanalysis.service.DfdlService2;
import com.example.javaairportflightanalysis.utils.GroupAndPageUtil;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class dfdlService2Impl implements DfdlService2 {

    @Resource
    DfdlMapper dfdlMapper;
    @Resource
    BaseApueMapper baseApueMapper;
    @Resource
    AirLineMapper airLineMapper;

    @Override
    public PageInfo getA(Integer pageNum, Integer pageSize) throws ParseException {
        ArrayList<DfdlDto2> a_takeoff = dfdlMapper.getA();
        //查出来三字码对应的中文名
        List<BaseApueCnnm> baseApueCnnm = baseApueMapper.getBaseApueCnnm();
        List<AirLine> airLines = airLineMapper.getAirLine();
        Map<String, List<DfdlDto2>> groupMap = GroupAndPageUtil.groupByAirlArptId2(a_takeoff);
        List<List<DfdlDto2>> groupList = new ArrayList<>(groupMap.values());
        List<DfdlDto2Merge> DfdlDto2Merges = processA2401Dtos2(groupList, airLines, baseApueCnnm);
        PageInfo pageInfo = GroupAndPageUtil.getPageInfo(DfdlDto2Merges,pageNum,pageSize);
        return pageInfo;
    }

    @Override
    public PageInfo getD(Integer pageNum, Integer pageSize) throws ParseException {
        ArrayList<DfdlDto2> d_takeoff = dfdlMapper.getD();
        //查出来三字码对应的中文名
        List<BaseApueCnnm> baseApueCnnm = baseApueMapper.getBaseApueCnnm();
        List<AirLine> airLines = airLineMapper.getAirLine();
        Map<String, List<DfdlDto2>> groupMap = GroupAndPageUtil.groupByAirlArptId2(d_takeoff);
        List<List<DfdlDto2>> groupList = new ArrayList<>(groupMap.values());
        List<DfdlDto2Merge> DfdlDto2Merges = processA2401Dtos2(groupList, airLines, baseApueCnnm);
        PageInfo pageInfo = GroupAndPageUtil.getPageInfo(DfdlDto2Merges,pageNum,pageSize);
        return pageInfo;
    }

    private List<DfdlDto2Merge> processA2401Dtos2(List<List<DfdlDto2>> groupList, List<AirLine> airLines, List<BaseApueCnnm> baseApueCnnm) throws ParseException {
        List<DfdlDto2Merge> mergeList = new ArrayList<>();
        for (List<DfdlDto2> DfdlDto2s : groupList) {
            StringBuilder sb = new StringBuilder();
            DfdlDto2Merge DfdlDto2Merge = new DfdlDto2Merge();
            String tempApcd1 = null;
            String tempApcd2 = null;
            String date1;
            // 用来存储Apno=2的数据
            if (DfdlDto2s.size() == 2) {
                for (DfdlDto2 dfdlDto2 : DfdlDto2s) {
                    //DfltFlno需要加上awcd
                    DfdlDto2Merge.setDfltFlno(dfdlDto2.getDfltAwcd() + dfdlDto2.getDfltFlno());
                    //DfltAwcd需要使用数据库替换
                    boolean flag = false;
                    for (AirLine airLine : airLines) {
                        if (Objects.equals(airLine.getCode(), dfdlDto2.getDfltAwcd())) {
                            DfdlDto2Merge.setDfltAwcd(airLine.getName());
                            flag = true;
                        }
                    }
                    if (!flag) {
                        DfdlDto2Merge.setDfltAwcd(dfdlDto2.getDfltAwcd());
                    }
                    String date = GroupAndPageUtil.getDate(dfdlDto2.getDtime());
                    DfdlDto2Merge.setDtime(date);
                    //当apno=1时，赋值Fptt
                    if (Objects.equals(dfdlDto2.getApno(), "1")) {
                        date1 = GroupAndPageUtil.getDate(dfdlDto2.getFptt());
                        DfdlDto2Merge.setFptt(date1);
                        //当apno=1时，赋值Apcd
                        boolean found = false;
                        for (BaseApueCnnm apueCnnm : baseApueCnnm) {
                            if (Objects.equals(apueCnnm.getCODE(), dfdlDto2.getApcd())) {
                                tempApcd1 = apueCnnm.getCNNM();
                                found= true;
                                break;
                            }
                        }
                        if (!found) {
                            tempApcd1 = dfdlDto2.getApcd();
                        }
                    }
                    date1 = GroupAndPageUtil.getDate(dfdlDto2.getFrtt());
                    DfdlDto2Merge.setFrtt(date1);
                    //当apno=2时，赋值Frtt
                    if (Objects.equals(dfdlDto2.getApno(), "2")) {

                        for (BaseApueCnnm apueCnnm : baseApueCnnm) {
                            if (Objects.equals(apueCnnm.getCODE(), dfdlDto2.getApcd())) {
                                tempApcd2 = (apueCnnm.getCNNM());
                            }
                            if (Objects.equals(dfdlDto2.getApcd(), "TSN")) {
                                tempApcd2 = "天津";
                            }
                        }
                    }
                }
                sb.append(tempApcd1).append("-").append(tempApcd2);
                DfdlDto2Merge.setApcd(sb.toString());
                mergeList.add(DfdlDto2Merge);
            }
        }
        return mergeList;
    }
}
